package com.app.service.impl;

import com.app.conf.WechatUtil;
import com.app.mapper.jiuquan.UserMapper;
import com.app.model.JiuquanUserModel;
import com.app.service.UserService;
import com.app.vo.JiuquanUserVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.security.auth.login.LoginException;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

//用户服务
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, JiuquanUserModel> implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public void update(JiuquanUserModel model) {
        userMapper.updateById(model);
    }

    @Override
    public void insert(JiuquanUserModel model) {
        userMapper.insert(model);
    }

    @Override
    public JiuquanUserModel selectById(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public JiuquanUserVo getList(Integer page, Integer limit, String keyword) {
        QueryWrapper<JiuquanUserModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        queryWrapper.like("name", keyword).or().like("phone", keyword);
        queryWrapper.orderByDesc("id");
        Page<JiuquanUserModel> pages = new Page<>();
        pages.setCurrent(page);
        pages.setSize(limit);
        Page<JiuquanUserModel> userModelPage = userMapper.selectPage(pages, queryWrapper);
        List<JiuquanUserModel> userList = userModelPage.getRecords();
        for (JiuquanUserModel userModel : userList) {
            userModel.setOpenId(null);
        }
        JiuquanUserVo vo = new JiuquanUserVo();
        vo.setList(userModelPage.getRecords());
        vo.setCount((int) userModelPage.getTotal());
        return vo;
    }

    @Override
    public void updateStatus(Integer id, Integer status) {
        JiuquanUserModel model = new JiuquanUserModel();
        model.setId(id);
        model.setStatus(status);
        userMapper.updateById(model);
    }

    @Override
    public void deleteById(Integer id) {
        userMapper.deleteById(id);
    }

    @Override
    public JiuquanUserModel getOpenid(String code, HttpServletRequest request) throws LoginException {
        //获取openid
        String openid = WechatUtil.getOpenid(code);
        if (openid == null) {
            throw new LoginException("openid为空");
        }
        //根据openid查询用户信息
        JiuquanUserModel user = userMapper.getByOpenid(openid);
        if (user == null) {
            //当前是新用户，自动注册
            user = new JiuquanUserModel();
            user.setStatus(1);
            user.setOpenId(openid);
        }
        return user;
    }

    @Override
    public String getPhone(String code, HttpServletRequest request) {
        String mobile = WechatUtil.getMobile(code);
        return mobile;
    }

    @Override
    public JiuquanUserModel getUserByOpenid(String openId) {
        QueryWrapper<JiuquanUserModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("open_id", openId);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public JiuquanUserModel selectUserById(Integer id) {
        QueryWrapper<JiuquanUserModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "name", "phone", "face_url", "status", "create_time", "update_time");
        queryWrapper.eq("id", id);
        return userMapper.selectOne(queryWrapper);
    }
}
